/*CopyRight 2015 By YXKJ Co., Ltd. All Rights Reserved.*/
package com.yx.ehis.system;

import java.io.File;
import java.util.List;
import org.eclipse.nebula.widgets.pshelf.PShelf;
import org.eclipse.nebula.widgets.pshelf.PShelfItem;
import org.eclipse.nebula.widgets.pshelf.RedmondShelfRenderer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import com.yx.system.menu.Item;
import com.yx.system.menu.Menu;
import com.yx.system.menu.MenuParser;
import com.yx.system.model.Resource;
import com.yx.system.util.SystemTools;

/**
 * 系统菜单
 * 
 * @author zgq
 */
public class SystemMenu extends ViewPart {
	public static final String ID = "com.yx.ehis.system.SystemMenu"; //$NON-NLS-1$

	public SystemMenu() {
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.VERTICAL));

		ScrolledComposite scrolledComposite = new ScrolledComposite(container,
				SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);

		Composite composite = new Composite(scrolledComposite, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.VERTICAL));
		init(composite);
		scrolledComposite.setContent(composite);
		scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT,
				SWT.DEFAULT));
	}

	private void init(Composite composite) {
		try {
			List<Menu> menus = MenuParser.getInstance().parse(
					new File(Resource.MENUS_PATH));
			if (menus.size() > 0) {
				PShelf shelf = new PShelf(composite, SWT.NONE);
				shelf.setRenderer(new RedmondShelfRenderer());
				for (Menu menu : menus) {
					PShelfItem shelfItem = new PShelfItem(shelf, SWT.NONE);
					shelfItem.setText(menu.getName());
					shelfItem.getBody().setLayout(new FillLayout(SWT.VERTICAL));
					if (!"".equals(menu.getIcon()))
						shelfItem
								.setImage(SystemTools.getImage(menu.getIcon()));
					List<Item> items = menu.getItems();
					if (items != null) {
						Tree tree = new Tree(shelfItem.getBody(), SWT.NONE);
						tree.setLinesVisible(true);
						tree.addSelectionListener(new SelectionAdapter() {
							@Override
							public void widgetSelected(SelectionEvent e) {
								Tree tree = (Tree) e.getSource();
								TreeItem treeItem = tree.getSelection()[0];
								treeItem.setExpanded(!treeItem.getExpanded());
								String vclass = (String) treeItem
										.getData("vclass");
								if (vclass != null && !"".equals(vclass)) {
									String type = (String) treeItem
											.getData("type");
									if ("view".equals(type)) {
										// 视图
										SystemTools.showView(vclass);
									} else if ("dialog".equals(type)) {
										// 对话框
										SystemTools.showDialog(vclass);
									} else {
										// 其它
									}
								}
							}
						});
						createTreeItem(items, tree);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void createTreeItem(List<Item> items, Object parent) {
		for (Item item : items) {
			TreeItem treeItem;
			if (parent instanceof Tree)
				treeItem = new TreeItem((Tree) parent, SWT.NONE);
			else
				treeItem = new TreeItem((TreeItem) parent, SWT.NONE);
			treeItem.setText(item.getName());
			if (!"".equals(item.getIcon()))
				treeItem.setImage(SystemTools.getImage(item.getIcon()));
			treeItem.setData("vclass", item.getVclass());
			treeItem.setData("type", item.getType());
			if (item.getItems() != null)
				createTreeItem(item.getItems(), treeItem);
		}
	}

	@Override
	public void setFocus() {
	}
}
